# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.pro>

_realname=cglm
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.8.4
pkgrel=2
pkgdesc="OpenGL Mathematics (glm) for C (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/recp/cglm"
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx_rtd_theme")
options=('!strip' 'staticlibs' '!buildflags')
source=("${_realname}-${pkgver}.tar.gz::https://github.com/recp/cglm/archive/v${pkgver}.tar.gz"
        'cglm-0.8.4-fix-win32-shared.patch')
sha256sums=('42f84c42c8a3e62954da77ab5c5d3264033d5802009e175db7921332a476126e'
            '9d7c75109fb734059e26d1fe70d2dc4d71c2df6e63fa9411bdb8a4009b11aea4')

prepare() {
    cd "${srcdir}/${_realname}-${pkgver}"

    patch -p1 -i "${srcdir}/cglm-0.8.4-fix-win32-shared.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}-static" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-static"
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCGLM_SHARED=OFF \
    -DCGLM_STATIC=ON \
    -DCGLM_USE_TEST=ON \
    ../${_realname}-${pkgver}
  cmake --build .

  [[ -d "${srcdir}/build-${MSYSTEM}-shared" ]] && rm -rf "${srcdir}/build-${MSYSTEM}-shared"
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DCGLM_SHARED=ON \
    -DCGLM_STATIC=OFF \
    -DCGLM_USE_TEST=ON \
    ../${_realname}-${pkgver}
  cmake --build .


  cd "${srcdir}/${_realname}-${pkgver}/docs"

  ${MINGW_PREFIX}/bin/sphinx-build source ${_realname}
}

check() {
  cd "${srcdir}/build-${MSYSTEM}-static"

  ctest . || true

  cd "${srcdir}/build-${MSYSTEM}-shared"

  ctest . || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR=${pkgdir} cmake --install .

  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR=${pkgdir} cmake --install .

  cd "${srcdir}/${_realname}-${pkgver}/docs"
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/doc
  cp -r ${_realname} "${pkgdir}${MINGW_PREFIX}"/share/doc/

  install -Dm644 "${srcdir}/${_realname}-${pkgver}"/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
